<template>
  <div class="app-container">
    <div v-permission="['version:create']" class="addbtn" @click="dialogTop('add')">新建版本</div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按名称搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="title"
          align="center"
          label="版本标题"
        />
        <el-table-column
          prop="isUpdater"
          align="center"
          label="是否强制更新"
        >
          <template slot-scope="{row}">
            <div>{{ row.isUpdater?'是':'否' }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="versionCode"
          align="center"
          label="版本号"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="创建时间"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="操作"
          width="220"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <div @click="detailDialog(row)">详情</div>
              <div v-permission="['version:eidt']" @click="edgeDialog(row)">编辑</div>
              <div @click="downFile(row)">下载
                <!-- <a :href="pathUrl+'/file/'+row.id">下载</a> -->
              </div>
              <!-- <div @click="deleteDialog(row)">删除</div> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>
    </div>
    <!-- 添加,编辑 -->
    <el-dialog :title="diaDelOrAdd?'添加版本':'编辑版本'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" custom-class="customClass">
      <el-form ref="addForm" :model="addForm" :rules="rules" auto-complete="on" label-position="left" style="width: 97%;">
        <el-form-item label="版本标题:" :label-width="formLabelWidth" prop="title">
          <el-input
            v-model="addForm.title"
            placeholder="请输入标题"
            name="title"
            maxlength="32"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="升级内容：" prop="content" :label-width="formLabelWidth">
          <el-input
            v-model="addForm.content"
            placeholder="请输入内容"
            name="content"
            maxlength="32"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="版本号：" :label-width="formLabelWidth" prop="versionCode">
          <el-input
            v-model="addForm.versionCode"
            placeholder="请输入版本号"
            name="versionCode"
            maxlength="32"
            tabindex="2"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="适应平台：" :label-width="formLabelWidth" prop="platform">
          <el-radio-group v-model="addForm.platform">
            <el-radio label="WEB">WEB</el-radio>
            <el-radio label="ANDROID">ANDROID</el-radio>
            <el-radio label="IOS">IOS</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item ref="upImg" label="上传安装包：" :label-width="formLabelWidth" prop="fileId">
          <div class="addbtn upclass">
            <el-upload
              ref="upload"
              class="upload-demo"
              :action="pathUrl+'/file/version'"
              :with-credentials="true"
              :show-file-list="false"
              :on-success="uploadSuccess"
            >
              上传
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="文件md5值：" :label-width="formLabelWidth" prop="fileMd5">
          <el-input
            v-model="addForm.fileMd5"
            placeholder="请输入"
            name="fileMd5"
            maxlength="32"
            tabindex="3"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="是否强制更新：" :label-width="formLabelWidth" prop="isUpdater">
          <el-radio-group v-model="addForm.isUpdater">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="footer">
        <div class="cancel" @click="dialogTop('cancel')">
          <span>取消</span>
        </div>
        <div v-if="diaDelOrAdd" class="sure" @click="createData">
          <span>确认</span>
        </div>
        <div v-else class="sure" @click="updataData">
          <span>确认</span>
        </div>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog title="版本详情" :visible.sync="detailDialogVisible" :close-on-click-modal="false" custom-class="customClass">
      <el-form label-position="left" style="width: 97%;">
        <el-form-item label="版本标题：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.title }}</span></div>
        </el-form-item>
        <el-form-item label="升级内容：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.content }}</span></div>
        </el-form-item>
        <el-form-item label="版本号：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.versionCode }}</span></div>
        </el-form-item>
        <el-form-item label="适应平台：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.platform }}</span></div>
        </el-form-item>
        <el-form-item label="需要强制更新：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.isUpdater?'是':'否' }}</span></div>
        </el-form-item>
        <el-form-item label="更新包下载：" :label-width="formLabelWidth">
          <div class="textName">
            <span class="addbtn upclass"><a :href="pathUrl+'/file/'+detailData.fileId">下载</a></span>
          </div>
        </el-form-item>
        <el-form-item label="文件md5值：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.fileMd5 }}</span></div>
        </el-form-item>
        <el-form-item label="创建时间：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.createAt }}</span></div>
        </el-form-item>
        <el-form-item label="修改时间：" :label-width="formLabelWidth">
          <div class="textName"><span>{{ detailData.updateAt }}</span></div>
        </el-form-item>
      </el-form>
      <div class="footer">
        <div class="cancel" @click="detailDialogVisible=false">
          <span>取消</span>
        </div>
      </div>
    </el-dialog>

    <!-- <delete-dialog
      :dislog-name="deleteObiect.name"
      :dialog-del-visible="dialogDelVisible"
      :title="'版本'"
      @funcancl="funcancl"
      @func="deleteData"
    /> -->

  </div>
</template>

<script>
import Pagination from '@/components/pagination'
// import DeleteDialog from '@/components/dialog/delete'
import { versionList, createVersion, updataVersion, detailVersion } from '@/api/version'
export default {
  name: 'Version',
  components: {
    Pagination
    // DeleteDialog
  },
  data() {
    return {
      pathUrl: process.env.VUE_APP_BASE_API,
      tableData: [],
      detailData: {},
      total: 0,
      listLoading: true,
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10
      },
      dialogFormVisible: false,
      dialogDelVisible: false,
      formLabelWidth: '120px',
      lablePosition: 'left',
      diaDelOrAdd: false,
      detailDialogVisible: false,
      addForm: {
        title: '',
        isUpdater: '',
        versionCode: '',
        fileMd5: '',
        fileId: '',
        content: '',
        platform: ''
      },
      deleteObiect: {},
      rules: {
        title: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        isUpdater: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        versionCode: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        fileMd5: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        fileId: [
          { required: true, message: '请上传安装包', trigger: 'change' }
        ],
        content: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        platform: [
          { required: true, message: '必填', trigger: 'change' }
        ]
      }
    }
  },
  computed: {

  },
  mounted() {
    this.getList()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    clean() {
      this.addForm = {
        title: '',
        isUpdater: '',
        versionCode: '',
        fileMd5: '',
        fileId: '',
        content: '',
        platform: ''
      }
    },
    funcancl(val) {
      this.dialogDelVisible = false
    },
    funsetcancl(val) {
      this.dialogResetVisible = false
    },
    downFile(row) {
      var url = this.pathUrl + '/file/' + row.id
      window.open(url)
    },
    // 获取数据
    async getList() {
      const res = await versionList(this.listQuery)
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
        this.listLoading = false
      }
    },
    dialogTop(val, row) {
      if (val === 'cancel') {
        this.diaDelOrAdd = false
        this.dialogFormVisible = false
      } else {
        this.diaDelOrAdd = true
        this.dialogFormVisible = true
      }
      this.clean()
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    edgeDialog(row) {
      this.diaDelOrAdd = false
      this.dialogFormVisible = true
      this.addForm = {
        id: row.id,
        title: row.title,
        isUpdater: row.isUpdater,
        versionCode: row.versionCode,
        fileMd5: row.fileMd5,
        fileId: row.fileId,
        content: row.content,
        platform: row.platform
      }
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    createData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await createVersion(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.clean()
            this.$message({
              type: 'success',
              message: '创建成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            console.log(res)
          }
        }
      })
    },
    updataData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await updataVersion(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: '编辑成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            console.log(res)
          }
        }
      })
    },
    // 删除
    deleteDialog(row) {
      this.dialogDelVisible = true
      this.deleteObiect = row
    },
    // async deleteData() {
    //   const id = this.deleteObiect.id
    //   const res = await deleteMachine(id)
    //   if (res.code === 200) {
    //     this.getList()
    //     this.dialogDelVisible = false
    //     this.$message({
    //       type: 'success',
    //       message: '删除成功!',
    //       showClose: true,
    //       center: true,
    //       duration: 2 * 1000
    //     })
    //   } else {
    //     console.log(res)
    //   }
    // },
    async detailDialog(row) {
      this.detailDialogVisible = true
      const id = row.id
      const res = await detailVersion(id)
      if (res.code === 200) {
        this.detailData = res.data
      } else {
        console.log(res)
      }
    },
    uploadSuccess(response, file, fileList) {
      if (response.code === 200) {
        this.addForm.fileId = response.data.id
        this.$refs.upImg.clearValidate()
        this.$message({
          type: 'success',
          message: '安装包上传成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.$refs.upload.clearFiles()
        this.$message({
          type: 'error',
          message: response.msg || 'error',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  .textName{
    font-size:14px;
    font-weight:400;
    color:rgba(247,181,0,0.8);
    span{
      font-size:14px;
      font-weight:400;
      color:rgba(255,255,255,0.8);
    }
  }
}
.upclass{
  margin: 0;
  background:linear-gradient(90deg,rgba(46,206,253,0.39) 0%,rgba(7,157,230,0.39) 100%);
}
</style>
